<template>
  <div class="container">
    <global-header :user="currentUser"></global-header>
    <form action="">
      <div class="mb-3">
        <label class="from-label">邮箱地址</label>
        <validate-input 
        :rules="emailRules" v-model="emailVal"
        placeholder="请输入邮箱地址"
        type="text"
        ></validate-input>
      {{emailVal}}
      </div>
      <div class="mb-3">
        <label for="exampleInputPassword1" class="form-label">密码</label>
        <input
          type="password"
          placeholder="请输入密码"
          class="form-control"
          id="exampleInputPassword1"
        />
      </div>
    </form>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, ref } from "vue";
import "bootstrap/dist/css/bootstrap.min.css";
import ColumnList, { ColumnProps } from "./components/ColumnLlist.vue";
import GlobalHeader, { UserProps } from "./components/GlobalHeader.vue";
import ValidateInput,{ RulesProp } from './components/ValidateInput.vue';
const currentUser: UserProps = {
  isLogin: true,
  name: "viking",
};
const testData: ColumnProps[] = [
  {
    id: 1,
    title: "123",
    // avatar: '../assets/logo.png',
    description: "123",
  },
  {
    id: 2,
    title: "12323",
    avatar: "123",
    description: "123",
  },
  {
    id: 2,
    title: "12323",
    avatar: "123",
    description: "123",
  },
  {
    id: 2,
    title: "12323",
    avatar: "123",
    description: "123",
  },
];
const emailReg = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/
export default defineComponent({
  name: "App",
  components: {
    // ColumnList,
    GlobalHeader,
    ValidateInput
  },
  setup() {
    // const inputRef = ref<any>()
    const emailVal = ref('')
    const emailRules: RulesProp = [
      { type:'required', message: '电子邮箱不能为空' },
      { type:'email', message: '请输入正确的电子邮箱格式' }
    ]
    const emailRef = reactive({
      val: "",
      error: false,
      message: ""
    });
    const validateEmail = () => {
      if(emailRef.val.trim() === ''){
        emailRef.error = true
        emailRef.message = 'can not be empty'
      } else if(!emailReg.test(emailRef.val)){
        emailRef.error = true
        emailRef.message = 'should be valid email'
      }
    };
    return {
      list: testData,
      currentUser,
      emailRef,
      validateEmail,
      emailRules,
      emailVal
    };
  }
});
</script>
